﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.AssetManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.Area.MonthcardServeice;
using static LJX.SmartPark.Service.IotInfo.LightingInfo.LightingInfoServeice;

namespace LJX.SmartPark.Service.AssetManage
{
    public class EquipmentLedgerServeice : IEquipmentLedgerServeice
    {
        private readonly IRepository<EquipmentLedger> _equipmentLedgerServeice;

        public EquipmentLedgerServeice(IRepository<EquipmentLedger> equipmentLedgerServeice)
        {
            _equipmentLedgerServeice = equipmentLedgerServeice;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteEquipmentLedgerAsync(EquipmentLedger input)
        {
            var result = await _equipmentLedgerServeice.DeleteByIdAsync(input.MapTo<EquipmentLedger>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _equipmentLedgerServeice.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fyEquipmentLedger"></param>
        /// <returns></returns>
        public async Task<Response<List<EquipmentLedger>>> GetEquipmentLedgerAsync(FyEquipmentLedger fyEquipmentLedger)
        {
            var equipmentLedger = await _equipmentLedgerServeice.GetAllListAsync();
            var result = equipmentLedger.MapToList<EquipmentLedger>();

            if (!string.IsNullOrEmpty(fyEquipmentLedger.equipmentName))
            {
                result = result.Where(a => a.EquipmentName.Contains(fyEquipmentLedger.equipmentName)).ToList();
            }

            if (!string.IsNullOrEmpty(fyEquipmentLedger.equipmentLocaId))
            {
                result = result.Where(a => a.EquipmentLocaId == int.Parse(fyEquipmentLedger.equipmentLocaId)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip((fyEquipmentLedger.pageIndex - 1) * fyEquipmentLedger.pageSize).Take(fyEquipmentLedger.pageSize).ToList();

            return Response<List<EquipmentLedger>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<EquipmentLedger> GetEquipmentLedgerById(EquipmentLedger input)
        {
            var equipment = _equipmentLedgerServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = equipment.MapTo<EquipmentLedger>();
            return Response<EquipmentLedger>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddEquipmentLedgerAsync(EquipmentLedger input)
        {
            var result = _equipmentLedgerServeice.InsertAsyncById(input.MapTo<EquipmentLedger>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdEquipmentLedgerAsync(EquipmentLedger input)
        {
            var result = _equipmentLedgerServeice.UpdateAsync(input.MapTo<EquipmentLedger>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 分页
        /// </summary>
        public class FyEquipmentLedger
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? equipmentName { get; set; }
            public string? equipmentLocaId { get; set; }
        }
    }
}
